$(document).ready(function () {
    //get draft document count and initial tables
    $.ajax({
        type: 'GET',
        url: '../document/count/draft',
        contentType: "application/json",
        dataType: 'JSON',
        success: function (json) {
            var draftCount = json.data.draft;
            var pagesNumber = parseInt(draftCount / 10 + 1);
            var $checkbox_paginate = $("#datatable-checkbox_paginate");
            var $ulObj = $(".pagination",$checkbox_paginate);

            createPaging($checkbox_paginate,pagesNumber);   //初始化分页元素
            displayDraftList(1);    //初始化表格

            var $liObj = $("li",$ulObj);

            for(var i = 1;i < $liObj.length - 1;i++){
                $liObj.eq(i).children("a").attr("onclick",'displayDraftList('+ i +')');
            }   //end for
        },
    });
    
});

function ReviewDocument(doc_id) {
    window.location.href　= "../document/my_draft_content.html?doc_id=" + doc_id;
}


//初始化表格
function initTable(data){
    var $datatable = $('#datatable-checkbox');
    $datatable.dataTable({
        data:data,
        "createdRow": function ( row, data, index ) {
            $('td', row).css("text-align","center").css("vertical-align","middle");
        },
        columns:[
            {"sTitle": "保存时间"},     //0
            {"sTitle": "公文标题"},     //1
            {"sTitle": "公文文种"},     //2
            {"sTitle": "公文文号"},     //3
            {"sTitle": "操作"},
        ],
        order:[
            [0, "desc"]
        ],
        columnDefs: [
            { orderable: false, targets: [0] },
        ],
        deferRender: true,
        paging:false
    } );

}


/* 显示草稿箱列表
 * @param:pagesNumber
 * */
function displayDraftList(pagesNumber) {
    clearTables($('#datatable-checkbox'));
    adjustPagination($('#pagination'),pagesNumber);

    //获取草稿箱列表
    $.ajax({
        type: 'POST',//请求类型
        url: '../document/list/draft',//请求地址
        data:JSON.stringify({
            "pageNumber": parseInt(pagesNumber)
        }),
        contentType: "application/json",
        dataType: 'JSON',
        success: function (json,status) {//成功获取到后台返回的值后，对后台返回的值做相应的一系列操作)
        var docs = json.data.documents;
        var datas = new Array();
        var arr = new Array();
        for(var i = 0;i<docs.length;i++){
            arr = [];
            arr[0] = docs[i].save_time;
            arr[1] = docs[i].title + "";
            arr[2] = docs[i].type;
            arr[3] = docs[i].number;
            arr[4] = "<button type='button' class='btn btn-success' onclick='ReviewDocument("+ docs[i].id + ")'>公文查看</button><br/>"
            datas[i] = arr;
        }
        initTable(datas);
    },
});
}